/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * @author Sergey I. Salishev
 */
package org.apache.wikimark;

/**
 * Abstract base class for Wiki engine driver
 */
public abstract class Driver implements PageFormatter, RequestFormatter {
	
	/**
	 * @return allowed variations in page size due to variable elements or 
	 * counters
	 */
	public abstract int getPageSizeVariation();
	
	private static String createHeaderBracket(char fill, int level,
			int hiLevelChars, int lowLevelChars) {

		final int cnt = hiLevelChars < lowLevelChars ? 
				Math.min(lowLevelChars,	level + hiLevelChars) 
				: Math.max(lowLevelChars, hiLevelChars - level);

		StringBuilder res = new StringBuilder(cnt);
		for (int i = 0; i < cnt; i++) {
			res.append(fill);
		}
		return res.toString();
	}

	/**
	 * Creates Wiki header string e.g. === text ===
	 * @param text header text
	 * @param level nesting level
	 * @param fill header fill char
	 * @param hiLevelChars number of fill chars in top level header bracket
	 * @param lowLevelChars number of fill chars in lowest level header bracket
	 * @param pair need both prefix and suffix brackets 
	 * @return header string
	 */
	static protected String createHeader(String text, int level, char fill,
			int hiLevelChars, int lowLevelChars, boolean pair) {

		String bracket = createHeaderBracket(fill, level, hiLevelChars,
				lowLevelChars);
		return bracket + " " + text + (pair ? " " + bracket : "") + "\r\n\r\n";
	}

}